package cn.jboa.dao.impl;

import cn.jboa.common.Constants;
import cn.jboa.dao.EmployeeDao;
import cn.jboa.entity.SysEmployee;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("empDao")
public class EmployeeDaoImpl extends BaseHibernateDaoSupport<SysEmployee> implements EmployeeDao {

    public EmployeeDaoImpl(){}

    @Autowired
    public EmployeeDaoImpl(@Qualifier("sessionFactory")SessionFactory sessionFactory){
        this.setSessionFactory(sessionFactory);
    }

    private static SysEmployee empGM = null;

    @Override
    public SysEmployee fineEmployeeBySN(String sn) {
        String hql="from SysEmployee e where e.sn=?";
        List<SysEmployee> result=this.getHibernateTemplate().find(hql,sn);
        return (result==null || result.size()==0)?null:result.get(0);
    }

    @Override
    public SysEmployee getManager(SysEmployee employee) {
        String hql=" from SysEmployee e where e.status=? and e.sysDepartment.id=? and e.sysPosition.nameCn=?";
        List<SysEmployee> emps=this.find(hql,Constants.EMPLOYEE_STAY,employee.getSysDepartment().getId(),Constants.POSITION_FM);
        if(emps==null||emps.size()==0)
            return null;
        return emps.get(0);
    }

    @Override
    public SysEmployee getCashier() {
        String hql = "from SysEmployee e where e.status = ? and e.sysPosition.nameCn = ?";
        return (SysEmployee) this.find(hql, Constants.EMPLOYEE_STAY,
                Constants.POSITION_CASHIER).get(0);
    }

    @Override
    public SysEmployee getGeneralManager() {
        if (empGM == null) {
            String hql = "from SysEmployee e where e.status = ? and e.sysPosition.nameCn = ?";
            empGM = (SysEmployee) this.find(hql, Constants.EMPLOYEE_STAY,
                    Constants.POSITION_GM).get(0);
        }
        return empGM;
    }
}
